//
//  FPExcessiveController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/5/9.
//

#import "FPExcessiveController.h"
#import "FPLoanController.h"
#import "FPProductListController.h"
#import "FPWebViewVC.h"

@interface FPExcessiveController ()

@property (weak, nonatomic) IBOutlet UIButton *borrowBtn;
@property (weak, nonatomic) IBOutlet UILabel *amountLab;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UIView *notifiView;
@property (weak, nonatomic) IBOutlet MarqueeLabel *marqueeLab;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topToHeight;
@property (weak, nonatomic) IBOutlet UIImageView *rotationImgView;
@property (weak, nonatomic) IBOutlet UILabel *tipLab1;
@property (weak, nonatomic) IBOutlet UILabel *tipLab2;

@property (nonatomic, strong) NSTimer *rotationTimer;

@end

@implementation FPExcessiveController

- (void)dealloc {

    [self invalidateRotationTimer];
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self invalidateRotationTimer];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 删除控制器
    NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    
    for (UIViewController *vc in marr) {
    
        if ([vc isKindOfClass:NSClassFromString(@"FPMeProfileTwoController")]) {
            
            [marr removeObject:vc];
            break;
        }
    }
    
    self.navigationController.viewControllers = marr;
}


- (void)initUI {
    
    self.topToHeight.constant = UIDevice.navigationFullHeight;
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    self.backView.backgroundColor = UIColor.whiteColor;
    [self setTitle:@"" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    self.borrowBtn.clipsToBounds = YES;
    self.borrowBtn.layer.cornerRadius = 23.f;
    self.borrowBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    self.notifiView.clipsToBounds = YES;
    self.notifiView.layer.cornerRadius = 10.5f;
    
    self.lineView.backgroundColor = [UIColor hexColor:@"#96C0FF"];
    
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab2.textColor = [UIColor hexColor:@"#3A3A3A"];
    
    self.marqueeLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.marqueeLab.text = @"互联网贷款仅限用于个人消费，不得用于购房、债务周转、股票等领域。借款后请保留资金消费凭证，必要时众鑫花会主动联系您对您的消费用途进行核查。";
    self.marqueeLab.fadeLength = 10.f;
    self.marqueeLab.animationDelay = 3.f;
    self.marqueeLab.font = FONT(12.f);
    
    [self.borrowBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
       
        [self toBorrowAction];
    }];
    
    [self startRotateAnimation];
    
    NSUInteger countDown = kRandomNum(2, 4);
    self.rotationTimer = [NSTimer scheduledTimerWithTimeInterval:countDown target:self selector:@selector(stopRotateAnimation) userInfo:nil repeats:NO];
}

- (void)startRotateAnimation {
    
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.toValue = @(M_PI * 2);
    rotation.duration = 3.0;
    rotation.repeatCount = MAXFLOAT;
    rotation.removedOnCompletion = NO;
    rotation.fillMode = kCAFillModeForwards;
    
    [self.rotationImgView.layer addAnimation:rotation forKey:@"rotationAnimation"];
}

- (void)stopRotateAnimation {
    
    [self.rotationImgView.layer removeAnimationForKey:@"rotationAnimation"];
    
    self.backView.hidden = YES;
    
    self.navView.backgroundColor = UIColor.clearColor;
    
    [self.marqueeLab restartLabel];
//    [self serverSideQueryInfoAPI];
}
#pragma mark - 用于服务端查询
- (void)serverSideQueryInfoAPI{
    [FPUserInfoModel outPushWithParam:@{} success:^(NSString * _Nonnull msg, NSInteger code) {
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
- (void)invalidateRotationTimer {
    
    if (self.rotationTimer) {
        
        [self.rotationTimer invalidate];
        self.rotationTimer = nil;
    }
}




#pragma mark - 获取剩余额度
- (void)getData {
    
    [FPHomeModel getHomePageInfoSuccess:^(NSInteger code, NSString * _Nonnull message, FPHomeModel * _Nonnull model) {
        
        if (code == 200) {
         
            self.model = model;
//            self.headerView.loanAmountLab.text = formatAmount([NSDecimalNumber decimalNumberWithString:model.totalCredit]);
        }

    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}



- (void)toBorrowAction {
    NSArray *arrayTestPhones = @[@"13709065567",@"17628247732",@"13548249124"];
    if ([arrayTestPhones containsObject:FPUserInfoModel.sharedUserInfo.phone]) {
        [MBManager hideLoading];
        [self showText:@"申请成功，请等待客服电话"];
    }else{
        [FPUserInfoModel outIdWithParam:@{} success:^(NSString * _Nonnull webUrl, NSString * _Nonnull msg, NSInteger code) {
            if (code == 200) {
                [MBManager hideLoading];
                FPWebViewVC *webViewVC = [FPWebViewVC new];
                webViewVC.strUrl = webUrl;
                [self.navigationController pushViewController:webViewVC animated:YES];
            } else {
                [MBManager hideLoading];
                [self showText:msg];
            }
        } failure:^(NSError * _Nonnull error) {
            [MBManager hideLoading];
            [self showError:error];
        }];
    }
//    [MBManager showLoading];
//    [FPUserInfoModel checkUserProfileInfoSuccess:^(NSInteger code, NSString * _Nonnull message , FPUserInfoModel *userInfoModel) {
//        
//        [FPUserInfoModel checkMatchProductsParam:userInfoModel isHome:NO success:^(NSInteger code, NSString * _Nonnull message, FPUserInfoModel *infoModel) {
//            [MBManager hideLoading];
//            
//            if (code == 200) {
//                
//                // 未匹配到产品
//                if (infoModel.succeed == NO) {
//                    
//                    // 更多产品(全部产品)
//                    [self moreProductAction];
//                }
//                else {
//                    
//                    FPLoanController *vc = [[FPLoanController alloc] init];
//                    vc.model = self.model;
//                    vc.productsList = infoModel.products;
//                    vc.infoModel = infoModel;
//                    [self.navigationController pushViewController:vc animated:YES];
//                }
//            }
//            else {
//                
//                [self showText:message];
//            }
//            
//        } failure:^(NSError * _Nonnull error) {
//            [self showError:error];
//        }];
//        
//    } failure:^(NSError * _Nonnull error) {
//        [self showError:error];
//    }];
}

#pragma mark - 查看更多 产品
- (void)moreProductAction {
    
    FPProductListController *vc = [[FPProductListController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
